package com.hnevc.scratchcarddemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView mImageView;
    private Bitmap alterBitmap;
    private float nx,ny;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.id_iv_card);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.card);

        alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());

        //获取屏幕分辨率
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        nx = (float)bitmap.getWidth()/dm.widthPixels;
        ny = (float) bitmap.getHeight() / dm.heightPixels;

        Matrix matrix = new Matrix();
        //matrix.setScale(nx,ny);
        Paint paint = new Paint();
        Canvas canvas = new Canvas(alterBitmap);
        canvas.drawBitmap(bitmap,matrix,paint);


        mImageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int x = (int) motionEvent.getX();
                int y = (int) motionEvent.getY();

                for(int i=-100;i<100;i++){
                    for (int j = -100; j <100 ; j++) {
                        if(Math.sqrt(i*i + j*j)<=100){
                            alterBitmap.setPixel((int) (x*nx)+i,(int)(y*ny+90)+j, Color.TRANSPARENT);
                        }
                    }
                }
                mImageView.setImageBitmap(alterBitmap);
                return true;
            }
        });
    }

}
